package logic

import (
	"context"
	"fmt"
	"person/model/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *personrpc.UserLoginRequest) (*personrpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	var finduser mysql.User
	mysql.DB.Debug().Where("phone =?", in.Phone).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("当前账号未登录")
	}
	if finduser.Password != in.Password {
		return nil, fmt.Errorf("当前密码错误")
	}
	return &personrpc.UserLoginResponse{Id: int64(finduser.ID)}, nil
}
